package com.senlan.water.controller.publicity;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senlan.water.common.result.BaseResult;
import com.senlan.water.dao.domain.PolicyPublicity;
import com.senlan.water.dao.service.PolicyPublicityService;
import com.senlan.water.dto.policy.PolicyPublicityAddDTO;
import com.senlan.water.dto.policy.PolicyPublicityEditDTO;
import com.senlan.water.dto.policy.PolicyPublicityExcelDTO;
import com.senlan.water.dto.policy.PolicyPublicitySearchDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 政策宣传控制器
 */
@RestController
@RequestMapping("/policy")
@Slf4j
@SaCheckLogin
public class PolicyPublicityController {

    @Resource
    private PolicyPublicityService policyPublicityService;

    /**
     * 新增
     * @param addDTO
     * @return
     */
    @PostMapping("add")
    public BaseResult<Boolean> add(@RequestBody PolicyPublicityAddDTO addDTO) {
        return BaseResult.data(policyPublicityService.addNew(addDTO));
    }

    /**
     * 编辑
     * @param editDTO
     * @return
     */
    @PostMapping("edit")
    public BaseResult<Boolean> edit(@RequestBody PolicyPublicityEditDTO editDTO) {
        return BaseResult.data(policyPublicityService.editById(editDTO));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("delete")
    public BaseResult<Boolean> delete(@RequestParam("id") Long id) {
        return BaseResult.data(policyPublicityService.deleteById(id));
    }

    /**
     * 查询全部
     * @return
     */
    @PostMapping("getAll")
    @SaIgnore
    public BaseResult<List<PolicyPublicity>> getAll() {
        return BaseResult.data(policyPublicityService.list());
    }

    /**
     * 分页查询
     * @param searchDTO
     * @return
     */
    @PostMapping("getPage")
    public BaseResult<Page<PolicyPublicity>> getPage(@RequestBody PolicyPublicitySearchDTO searchDTO) {
        return BaseResult.data(policyPublicityService.getPage(searchDTO));
    }

    /**
     * 导出excel
     */
    @PostMapping("export")
    public void exportExcel(HttpServletResponse response, @RequestBody List<Long> ids) {
        List<PolicyPublicity> list = CollectionUtil.isEmpty(ids)? policyPublicityService.list() : policyPublicityService.listByIds(ids);

        //组装excel数据
        List<PolicyPublicityExcelDTO> excelDataList = new ArrayList<>();
        for (PolicyPublicity policyPublicity : list){
            PolicyPublicityExcelDTO excelDTO = new PolicyPublicityExcelDTO();
            BeanUtil.copyProperties(policyPublicity,excelDTO);
            excelDataList.add(excelDTO);
        }
        //导出excel
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("政策宣发", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(), PolicyPublicityExcelDTO.class).sheet("模板").doWrite(excelDataList);
        } catch (Exception e) {
            log.error("导出excel失败{}",e.getMessage(),e);
        }
    }

}
